package com.mantou.mantouaiagent.util;


import com.mantou.mantouaiagent.entities.SpringSecurityUserDetails;
import com.mantou.mantouaiagent.exp.BusinessException;
import com.mantou.mantouaiagent.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @auther zzyy
 * @create 2025-01-05 16:17
 */
@Slf4j
public class SpringSecutiryUtil
{
    /**
     * 从SecurityContextHolder上下文获得到当前登录用户信息
     * @return
     */
    public static SpringSecurityUserDetails getCurrentUser()
    {
        SpringSecurityUserDetails userDetails = null;


        try
        {
            userDetails = (SpringSecurityUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            log.info("当前登录用户信息：{}", userDetails);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ReturnCodeEnum.GET_SPRINGSECURITYUSERINFO_ERROR.getCode(),
                    ReturnCodeEnum.GET_SPRINGSECURITYUSERINFO_ERROR.getMessage());
        }

        return userDetails;
    }
}
